//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Windows.Media.Effects;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public abstract class ShapeBase : IAnchored, INotifyPropertyChanged
    {
        public abstract ShapeProperties ShapeProperties { get; set; }

        public double RotationAngle
        {
            get
            {
                if (ShapeProperties.Xfrm == null)
                    return 0;
                return ShapeProperties.SafeXfrm.RotationAngle;
            }
            set { ShapeProperties.SafeXfrm.RotationAngle = value; }
        }

        public  double? Width
        {
            get
            {
                if (ShapeProperties.Xfrm == null)
                    return null;
                return ShapeProperties.SafeXfrm.Size.Width;
            }
            set
            {
                if (value == null)
                    return;
                ShapeProperties.SafeXfrm.Size.Width = value.Value;
                NotifyPropertyChanged("Width");
            }
        }

        public   double? Height
        {
            get
            {
                if (ShapeProperties.Xfrm == null)
                    return null;
                return ShapeProperties.SafeXfrm.Size.Height;
            }
            set
            {
                if (value == null)
                    return;
                ShapeProperties.SafeXfrm.Size.Height = value.Value;
                NotifyPropertyChanged("Height");
            }
        }

        #region IAnchored Members

        public void ReadXml(ReadContext context)
        {
            ReadXmlCore(context);
        }

        public void WriteXml(WriteContext context)
        {
            WriteXmlCore(context);
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected abstract void ReadXmlCore(ReadContext context);

        protected abstract void WriteXmlCore(WriteContext context);

        public BitmapEffect BitmapEffect(ColorScheme colorScheme)
        {
            {
                if (ShapeProperties == null)
                    return null;
                var l = ShapeProperties.EffectProperties as EffectList;
                if (l == null)
                    return null;
                return l.BitmapEffect(colorScheme);
            }
        }


        protected void NotifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}